package cn.com.jit;

import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.expr.StringLiteralExpr;
import com.github.javaparser.ast.body.FieldDeclaration;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

public class JavaFileProcessor implements FileProcessor {

    @Override
    public List<SqlVo> processFile(File file) {
        // 读取指定路径Java文件的全部内容
        String code = null;
        try {
            code = new String(Files.readAllBytes(file.toPath()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        CompilationUnit cu;
        try {
            // 使用StaticJavaParser解析Java代码
            cu = StaticJavaParser.parse(code);
        } catch (Exception e) {
            // 捕获解析过程中的异常，并输出错误信息
            System.err.println("Error parsing Java code."+file.getPath());
            return null;
        }

        // 查找代码中所有字符串字面量
        List<StringLiteralExpr> stringLiterals = cu.findAll(StringLiteralExpr.class);
        //定义一个SqlVo 对象
        SqlVo sqlVo = null;
        //定义一个List封闭SqlVo的变量
        List<SqlVo> sqlVoList = new ArrayList<>();
        for (StringLiteralExpr literal : stringLiterals) {
            // 获取字符串字面量的值
            String sqlCandidate = literal.asString();
            // 获取字符串字面量所在的行号
            int lineNo = literal.getBegin().map(position -> position.line).orElse(-1);
            // 检查字符串是否包含SQL关键字
            if (sqlCandidate.contains("SELECT") || sqlCandidate.contains("INSERT")
                    || sqlCandidate.contains("UPDATE") || sqlCandidate.contains("DELETE")) {
                // 输出潜在的SQL语句及其所在行号
                sqlVo = new SqlVo(file.getPath(),sqlCandidate,lineNo);
                sqlVoList.add(sqlVo);
//                System.out.println("Potential SQL at line " + lineNo + ": " + sqlCandidate);
            }
        }

        //从注解中获取表名、字段名还没有加进来，返回值不同，需要再完善
/*
        List<ClassOrInterfaceDeclaration> classOrInterfaceDeclarations = cu.findAll(ClassOrInterfaceDeclaration.class);
        for (ClassOrInterfaceDeclaration classOrInterfaceDeclaration : classOrInterfaceDeclarations) {
            // 获取类名
            String className = classOrInterfaceDeclaration.getNameAsString();
            // 输出类名
            System.out.println("Class name: " + className);
        }
*/
        return sqlVoList;
    }
    private String getTableName(ClassOrInterfaceDeclaration n) {
        String tableName = n.getNameAsString();
        if (n.getAnnotationByName("Table").isPresent()) {
            StringLiteralExpr tableExpr = n.getAnnotationByName("Table").get().asStringLiteralExpr();
            tableName = tableExpr.getValue();
        }
        return tableName;
    }

    private List<String> getFieldInfo(ClassOrInterfaceDeclaration n) {
        List<String> fieldInfos = new ArrayList<>();
        for (FieldDeclaration field : n.getFields()) {

            if (field.getAnnotationByName("Id").isPresent()) {
                StringLiteralExpr columnExpr = field.getAnnotationByName("Id").get().asStringLiteralExpr();
                fieldInfos.add(columnExpr.getValue());
            }

            if (field.getAnnotationByName("Column").isPresent()) {
                StringLiteralExpr columnExpr = field.getAnnotationByName("Column").get().asStringLiteralExpr();
                fieldInfos.add(columnExpr.getValue());
            }

//            fieldInfo.setFieldName(field.getVariable(0).getNameAsString());
        }
        return fieldInfos;
    }

}
